import { useQuery } from "@apollo/client";
import { getUserInfo_service } from "../graphql/user";
import { useState } from "react";

export interface userInfoType {
  tel: string;
  id: string;
  desc: string;
  name: string;
  avatar: string;
}

export interface useUserInfoType {
  data: userInfoType | undefined;
  loading: boolean;
  refetch: () => any;
}

function useUserInfo(): useUserInfoType {
  const [data, setData] = useState<userInfoType>();
  const { loading, refetch } = useQuery(getUserInfo_service, {
    onCompleted(data: { getUserInfo: userInfoType }) {
      const { tel, id, desc, name, avatar } = data.getUserInfo;
      setData({
        tel,
        id,
        desc,
        name,
        avatar,
      });
    },
    onError() {
      // message.error("网络连接错误，请重试！");
      return {
        data: undefined,
        loading: true,
        refetch: () => {},
      };
    },
  });
  return { data, loading, refetch };
}

export default useUserInfo;
